package com.yanqu.road.server.cmd.marriage;

import com.yanqu.road.pb.marriage.MarriageProto;
import com.yanqu.road.pb.player.PlayerProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.marriage.MarriageMgr;
import com.yanqu.road.server.manger.player.UserMgr;
import com.yanqu.road.server.pb.PlayerPb;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.server.command.Cmd;

import java.util.ArrayList;
import java.util.List;

@Cmd(code = Protocol.S_MARRIAGE_RECOMMEND_PLAYER, desc = "获取指定联姻推荐玩家列表")
public class MarriageRecommendPlayerCmd extends AbstractCommand {

    private String childUid;

    private List<Long> userIdList;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        MarriageProto.MarriageRecommendPlayerReqMsg reqMsg = MarriageProto.MarriageRecommendPlayerReqMsg.parseFrom(packet.getBytes());
        childUid = reqMsg.getChildUid();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        userIdList = new ArrayList<>();
        //前在线的活跃玩家至5名  相差不超过1个身份，同时有超过1个处于未联姻状态的异性子嗣。
        List<Long> recommendPlayer = MarriageMgr.recommendPlayer(player);
        userIdList.addAll(recommendPlayer);
    }

    @Override
    public void sendData(GamePlayer player) {
        MarriageProto.MarriageRecommendPlayerRespMsg.Builder respMsg = MarriageProto.MarriageRecommendPlayerRespMsg.newBuilder();
        if(null != userIdList) {
            for (long userId : userIdList) {
                PlayerProto.PlayerSimpleTempMsg.Builder userMsg = PlayerPb.parsePlayerSimpleTempMsg(UserMgr.getUserInfo(userId));
                if (null != userMsg) {
                    respMsg.addDataList(userMsg);
                }
            }
        }
        player.sendPacket(Protocol.U_MARRIAGE_RECOMMEND_PLAYER, respMsg);
    }
}
